<template>
    <div>
        <!-- 面包屑导航区 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片视图区域 -->
        <el-card>
            <!-- 商品搜索框 -->
            <el-row :gutter="20">
                <el-col :span="10">
                    <el-input placeholder="请输入内容"  clearable v-model="queryInfo.query" @clear="getGoodsList">
                        <el-button  icon="el-icon-search" slot="append" @click="getGoodsList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click='goAddPage'>添加商品</el-button>
                </el-col>
            </el-row>

            <!-- 商品表格展示 -->
            <el-table :data="goodsList" border stripe>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="商品名称" prop="goods_name"></el-table-column>
                <el-table-column label="商品价格(元)" prop="goods_price" width="95px"></el-table-column>
                <el-table-column label="商品重量" prop="goods_weight" width="95px"></el-table-column>
                <el-table-column label="创建时间" prop="add_time" width="140px">
                    <template slot-scope="scope">
                        {{scope.row.add_time | dateFormat}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="125px">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" icon="el-icon-edit" @click="goEditPage(scope.row.goods_id)"></el-button>
                        <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteGoods(scope.row.goods_id)"></el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[3, 5, 10, 15]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total" background>
            </el-pagination>
        </el-card>

    </div>
</template>

<script>
export default {
  data () {
    return {
      // 查询商品列表
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表数据
      goodsList: [],
      // 商品总量
      total: 0

    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表数据
    async getGoodsList () {
      const { data: res } = await this.$axios.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取商品数据失败')
      this.$message.success('获取商品列表成功')
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 分页 控制每页显示商品数量
    handleSizeChange (newSize) {
      // 参数newSize每页显示数量，赋值给pagesize，在从新获取数据
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 分页，控制页码，即到哪一页
    handleCurrentChange (newNum) {
      this.queryInfo.pagenum = newNum
      this.getGoodsList()
    },
    // 删除商品功能
    async deleteGoods (id) {
      // 弹窗提示是否确认删除
      const result = await this.$confirm('是否确认删除该商品？', '删除警告', {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 判断result是否和confirm相等，，不相等取消删除
      if (result !== 'confirm') return this.$message.info('已取消删除')
      // 若相等即确认删除，发送删除请求
      const { data: res } = await this.$axios.delete('goods/' + id)
      if (res.meta.status !== 200) return this.$message.error('删除商品失败')
      this.$message.success('删除商品成功')
      this.getGoodsList()
    },
    // 添加商品按钮。点击跳转到添加页面
    goAddPage () {
      this.$router.push('/goods/add')
    },

    // // 控制商品编辑弹框显示，获取编辑商品数据

    goEditPage (id) {
      this.$router.push({ path: '/goods/edit', query: { goods_id: id } })
    }
  }
}
</script>
<style lang="less" scoped>

</style>
